package com.example.peibei.ui.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.dingtao.common.bean.User;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

public class PaidanListFragment extends WDFragment {

    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager2 viewpager;

    String type;

    public PaidanListFragment setType(String type) {
        this.type = type;
        return this;
    }

    @Override
    public String getPageName() {
        return "paidan";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_paidan_list;
    }

    @Override
    protected void initView() {
        int index = 0;
        if (getArguments() != null) {
            index = getArguments().getInt("index", 0);
        }
        ArrayList<Fragment> fragments = new ArrayList<>();

        User loginUser = SpConstant.getLoginUser();
        boolean dispatch = loginUser.isExistsDispatchOrder();
        boolean apply = loginUser.isExistsApplyOrder();

        if (dispatch){
            fragments.add(new PaidanList1Fragment().setType(type));
        }
        if (apply){
            fragments.add(new PaidanList2Fragment().setType(type));
        }
        viewpager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });
        viewpager.setOffscreenPageLimit(fragments.size());
        viewpager.setUserInputEnabled(false);
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewpager.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        if (fragments.size() > 1){
            tablayout.setVisibility(View.VISIBLE);

            if (index > 0) {
                if (index >= fragments.size()){
                    index = fragments.size() - 1;
                }
                tablayout.selectTab(tablayout.getTabAt(index));
                viewpager.setCurrentItem(index, false);
            }
        }


    }
}
